//
//  SiriManageVC.swift
//  UNHimalayas
//
//  Created by 王水平 on 2019/11/27.
//  Copyright © 2019 HachiTech. All rights reserved.
//

import UIKit

class SiriManageVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView : UITableView!
    var dataArray = [[String:String]()]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.edgesForExtendedLayout = []
        self.navigationItem.title = "Siri捷径管理"
        self.view.backgroundColor = RGBA(240, g: 240, b: 240, a: 1.0);
        
        initAction()
        initTabelView()
        
        dataArray = [
            ["title":"继续播放","imgName":"XMChat_group_ic_collection"],
            ["title":"进入我的下载","imgName":"XMChat_group_ic_collection"],
            ["title":"打开我的订阅","imgName":"XMChat_group_ic_collection"],
            ["title":"打开我的历史","imgName":"XMChat_group_ic_collection"],
            ["title":"语音搜索","imgName":"XMChat_group_ic_collection"],
          ]
        tableView.reloadData()
    }
    
    func initAction() -> Void {
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.normal)
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.highlighted)
           
        leftBtn.addTarget(self, action: #selector(SiriManageVC.leftAction), for: UIControl.Event.touchUpInside)
        // button自适应大小
        leftBtn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView:leftBtn)
    }
    
    func initTabelView() -> Void {
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0), style: .plain)
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.tableFooterView = UIView()
        self.view.addSubview(self.tableView)
        
        self.tableView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.view)
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellName = "SiriManageViewCellName"
        let cell = SiriManageViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellName)
        
        let dict = dataArray[indexPath.row]
        
        cell.imgView.image = UIImage(named: dict["imgName"]!)
        cell.titleLabel.text = dict["title"]
        
        return cell;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kRealWidth(60)
    }
    
    //下划线长度控制
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
            
            if indexPath.row == dataArray.count - 1 {

                cell.layoutMargins = UIEdgeInsets.zero
            }
            else {
                cell.layoutMargins = UIEdgeInsets(top: 0, left: kRealWidth(60), bottom: 0, right: 0)
            }
        }
        if cell.responds(to:  #selector(setter: UITableViewCell.separatorInset)) {
            
            if indexPath.row == dataArray.count - 1 {
                
                cell.separatorInset = UIEdgeInsets.zero
            }
            else {
                cell.separatorInset = UIEdgeInsets(top: 0, left: kRealWidth(60), bottom: 0, right: 0)
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
    }

    // MARK: - 点击
    
    @objc private func leftAction() -> Void {
        
        self.navigationController?.popViewController(animated: true)
    }

}
